#include "ObjectMgr.h"

int ObjectMgr::TickAllObject()
{
	if (m_iLastTickTime == SysTime())
	{
		return 0;
	}
	m_iLastTickTime = SysTime();

	ObjectMap::iterator it = m_stObjectMap.begin();
	for (; it != m_stObjectMap.end(); ++it)
	{
		Object* pObject = it->second;
		if (pObject)
		{
			pObject->Tick();
		}
	}
	return 0;
}

Player* ObjectMgr::CreatePlayer()
{
	Player* pPlayer = new Player();
	if (NULL == pPlayer)
	{
		return NULL;
	}

	_AddToManager(pPlayer);

	return pPlayer;
}

Monster* ObjectMgr::CreateMonster()
{
	Monster* pMonster = new Monster();
	if (NULL == pMonster)
	{
		return NULL;
	}

	_AddToManager(pMonster);

	return pMonster;
}

int ObjectMgr::DestroyObject(Object* pObject)
{
	if (NULL == pObject)
	{
		return -1;
	}

	_DelFromManager(pObject);

	delete pObject;

	return 0;
}

Object* ObjectMgr::GetObjectByID(int iID)
{
	ObjectMap::iterator it = m_stObjectMap.find(iID);
	if (it == m_stObjectMap.end())
	{
		return NULL;
	}

	return it->second;
}

void ObjectMgr::_AddToManager(Object* pObject)
{
	if (pObject)
	{
		pObject->SetObjectID(m_iMaxID);
		m_iMaxID++;
		m_stObjectMap[pObject->GetObjectID()] = pObject;
	}
}

void ObjectMgr::_DelFromManager(Object* pObject)
{
	if (pObject)
	{
		m_stObjectMap.erase(pObject->GetObjectID());
	}
}

